<template>
    <div>
        <div id="slider" class="mui-slider">
            <div id="sliderSegmentedControl" class="mui-scroll-wrapper mui-slider-indicator mui-segmented-control mui-segmented-control-inverted">
                <div class="mui-scroll">
                    <a
                            @click="getPhotoList(item.id)"
                            v-for="item in cates" :key="item.id"
                            :class="['mui-control-item', item.id === 0? 'mui-active':'']">
                        <!-- 初次渲染时，进行判断，id是否等于0，是[全部]选项就加上 -->
                        {{ item.title }}
                    </a>
                </div>
            </div>
        </div>

        <!-- 图片列表 -->
        <ul class="photo-list">
            <router-link tag="li" v-for="item in list"
                         :key="item.id" :to="'/home/photoinfo/' + item.id">
                <img v-lazy="item.img_url">

                <div class="info">
                    <h4 class="info-title">{{ item.title }}</h4>
                    <p class="info-body">{{ item.zhaiyao }}</p>
                </div>
            </router-link>
        </ul>

    </div>
</template>

<script>
    // 1.导入mui的js文件
    import mui from '../../../../lib/mui/js/mui.js'

    export default {
        name: "",
        data(){
            return {
                cates: [], //所有图片分类
                list: []  //每个分类的图片数组
            }
        },
        created(){
            this.getAllCategory()
            // 默认进入页面，主动请求 所有图片列表的数组
            this.getPhotoList(0)
        },
        mounted(){
            // 组件尚未挂载到页面中，初始化无意义，所以要将其放在mounted()方法中
            // 2.初始化滑动空间
            mui('.mui-scroll-wrapper').scroll({
                deceleration: 0.0005 //flick 减速系数，系数越大，滚动速度越慢，滚动距离越小，默认值0.0006
            });
        },
        methods: {
            // 获取所有图片分类
            getAllCategory(){
                this.$http.get('static/data/cates.json').then(res => {
                    if(res.body.status === 0){
                        res.body.message.unshift({
                            id: 0,
                            title: '全部'
                        })
                        this.cates = res.body.message
                    }
                })
            },
            getPhotoList(id){
                this.$http.get('static/data/cateImages.json').then(res => {
                    if(res.body.status === 0){
                        var showList = res.body.message.find(item => item.id === id)
                        this.list = showList.images
                    }
                })
            }
        }
    }
</script>

<style scoped>
    *{
        touch-action:pan-y;
    }

    .photo-list{
        margin: 0;
        padding: 10px 10px 0;

        list-style: none;
    }
    .photo-list li{
        text-align: center;
        background-color: #CCCCCC;
        margin-bottom: 8px;
        box-shadow: 0 0 9px #999999;
        position: relative;
    }
    .photo-list li img{
        display: block;
        width: 100%;
    }

    .info{
        position: absolute;
        bottom: 0;
        left: 0;
        right: 0;
        color: white;
        text-align: left;
        padding: 0 6px;
        background-color: rgba(0, 0, 0, 0.4);
        max-height: 84px;
    }

    .info-title{
        font-size: 16px;
    }

    .info-body{
        color: white;
        font-size: 14px;
    }

    /* 懒加载图片样式 */
    img[lazy=loading] {
        width: 40px;
        height: 300px;
        margin: auto;
    }
</style>